package com.zlwy.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zlwy.application.GlobalApplication;
import com.zlwy.debug.Logger;
import com.zlwy.entity.C_Result;
import com.zlwy.entity.Grid;
import com.zlwy.entity.K_CandleEntity;
import com.zlwy.http.K_DaysRequest;
import com.zlwy.http.K_MonthRequest;
import com.zlwy.http.K_WeeksRequest;
import com.zlwy.util.DateUtil;
import com.zlwy.util.Util;
import com.zlwy.view.CandleView;
import com.zlwy.view.VectorCandleView;
/**
 * K线图视图类
 * 蜡烛线
 * @author acer
 *
 */
public class CandleFragment extends Fragment implements OnClickListener{
	
	private View candleView;
	private List<K_CandleEntity> datas = new ArrayList<K_CandleEntity>();
	private List<K_CandleEntity> tens = new ArrayList<K_CandleEntity>();
	private List<K_CandleEntity> days = new ArrayList<K_CandleEntity>();
	private List<K_CandleEntity> weeks = new ArrayList<K_CandleEntity>();
	private List<K_CandleEntity> months = new ArrayList<K_CandleEntity>();
	private TextView tvClose;//收盘价
	private TextView tvRange;//涨跌幅
	private TextView tvHigh;//最高价
	private TextView tvLow;//最低价
	private TextView tvOpen;//开盘价
	
	
	private TextView tvHighst;//最高
	private TextView tvSecond;//第二档
	private TextView tvThird;//第三档
	private TextView tvFour;//第四档
	private TextView tvLowst;//最低
	
	private TextView tvCNum;//成交量
	private TextView tvCTm;
	
	private Button btnTen;
	private Button btnDay;
	private Button btnWeek;
	private Button btnMonth;
	private int showChart = 0;
	private CandleView mCandle;
	private VectorCandleView vectorView;
	private Timer timer;
	private int tId;
	private String svTime;//系统时间
	private int moveIndex = 0;
	
	private BroadcastReceiver tenReceiver = new BroadcastReceiver(){

		public void onReceive(Context context, Intent intent) {
			String id = intent.getStringExtra("ID");
			String utcTm = intent.getStringExtra("STM");
			svTime =Util.utc2Local(utcTm);
				try {
					JSONObject json = new JSONObject(intent.getStringExtra("JSON")); 
					JSONArray arr = null;
					if(json!=null&&!json.isNull("msg")&&!json.getString("msg").trim().equals("{}")){
						 arr = json.getJSONArray("msg");
					}
					if(arr!=null)
					if(arr.length()>0){
						tens.clear();
						for(int i=0; i<arr.length(); i++){
							JSONObject obj = arr.getJSONObject(i);
							if(obj!=null){
								K_CandleEntity candle = new K_CandleEntity();
								if(!obj.isNull("open_prc")){			
									candle.setOpen(Double.parseDouble(Util.dealStrs(obj.getString("open_prc"), 3)));
								}
								if(!obj.isNull("close_prc")){		
									candle.setClose(Double.parseDouble(Util.dealStrs(obj.getString("close_prc"), 3)));
								}
								if(!obj.isNull("max_prc")){
									candle.setHigh(Double.parseDouble(Util.dealStrs(obj.getString("max_prc"), 3)));
								}
								if(!obj.isNull("min_prc")){
									candle.setLow(Double.parseDouble(Util.dealStrs(obj.getString("min_prc"), 3)));
								}
								if(!obj.isNull("sum_num")){
									candle.setSum_num(Double.parseDouble(Util.dealStrs(obj.getString("sum_num"), 3)));
								}
								if(!obj.isNull("sum_prc")){
									candle.setSum_prc(Double.parseDouble(Util.dealStrs(obj.getString("sum_prc"), 3)));
								}
								if(!obj.isNull("tm")){
									candle.setDate(Util.utc2Local(obj.getString("tm")));
								}	
								if(!obj.isNull("quote_change")){
									candle.setUdRange(obj.getString("quote_change"));
								}else{
									candle.setUdRange("0");
								}	
								tens.add(candle);
							}
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				mHandler.sendEmptyMessage(10);				
		}		
	};
	
	private BroadcastReceiver moveReceiver = new BroadcastReceiver(){
		public void onReceive(Context context, Intent intent) {
			moveIndex = intent.getIntExtra("INDEX", datas.size()-1);
			mHandler.sendEmptyMessage(11);
		}		
	};
	
	private BroadcastReceiver t_infoReceiver = new BroadcastReceiver(){
		public void onReceive(Context context, Intent intent) {
			mHandler.sendEmptyMessage(12);
		}		
	};
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 0:
					//无网络
					Toast.makeText(getActivity(), "连接服务器失败，请检查网络", Toast.LENGTH_SHORT).show();
					break;
				case 1:
					//获取日K成功,展示日K
//					datas.clear();
					Logger.i("", "---日---");
					datas = days;
					setPeakValue(datas);
					setValues(datas.size()-1);					
					mCandle.setData(datas);
					setMaxNum(datas);
					break;
				case 2:
					Logger.i("", "---周---");
					datas = weeks;
					setPeakValue(datas);
					setValues(datas.size()-1);					
					mCandle.setData(datas);
					setMaxNum(datas);
					break;
				case 3:
					Logger.i("", "---月---");
					datas = months;
					setPeakValue(datas);
					setValues(datas.size()-1);
					mCandle.setData(datas);
					setMaxNum(datas);
					break;
				case 10:
					//获取10分钟数据成功
					Logger.i("", "接收到10分钟数据广播！");
					if(showChart==0){
						//接收到十分钟图广播，更新K线图
						int size = tens.size();
						if(size==1){
							//证明只有一条数据
							tens.get(0).setDate(postponeOpenTime(tens.get(0).getDate()));
							onlyOneData();
						}
						if(size>1){
							//证明有多条数据,要计算每个数据之间有没有时差
							tens.get(0).setDate(postponeOpenTime(tens.get(0).getDate()));
							moreThanOneData();
						}
						datas = tens;
						setPeakValue(datas);
						setValues(datas.size()-1);
						mCandle.setData(datas);
						setMaxNum(datas);
					}					
					break;
				case 11:
					//接收到移动广播
					setValues(moveIndex);
					break;
				case 12:
					sendTenWebSocketMessage();
					break;
			}
		};
	};

	@SuppressLint("InflateParams")
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(candleView==null){
			candleView = inflater.inflate(R.layout.candle_view, null);
			initView();
			initTenReceiver();
			initMoveReceiver();	
			initT_InfoReceiver();
		}	
		return candleView;
	}
	
	private void initTenReceiver(){
		IntentFilter filter = new IntentFilter();
		filter.addAction("COM.ZLWY.PUSH_TEN");
		getActivity().registerReceiver(tenReceiver, filter);
	}
	
	private void initT_InfoReceiver(){
		IntentFilter filter = new IntentFilter();
		filter.addAction("COM.ZLWY.T_INFO_OK");
		getActivity().registerReceiver(t_infoReceiver, filter);
	}
	
	private void initMoveReceiver(){
		IntentFilter filter = new IntentFilter();
		filter.addAction("COM.ZLWY.CANDLEMOVE");
		getActivity().registerReceiver(moveReceiver, filter);
	}
	
	/**
	 * 绘制时间文本
	 * @param index
	 * @param x
	 */
	private void drawTime(int index, double x){
		candleView.draw(new Canvas(){
		
		});
	}
	/**
	 * 初始化视图
	 */
	private void initView(){
		
		tvClose = (TextView) candleView.findViewById(R.id.tvCandleEqually);
		tvRange = (TextView) candleView.findViewById(R.id.tvCandleRange);
		tvHigh = (TextView) candleView.findViewById(R.id.tvCandleHigh);
		tvLow = (TextView) candleView.findViewById(R.id.tvCandleLow);
		tvOpen = (TextView) candleView.findViewById(R.id.tvCandleOpen);
		
		tvHighst = (TextView) candleView.findViewById(R.id.tvCandleHighRange);
		tvSecond = (TextView) candleView.findViewById(R.id.tvCandleSecondRange);
		tvThird = (TextView) candleView.findViewById(R.id.tvCandleThreeRange);
		tvFour = (TextView) candleView.findViewById(R.id.tvCandleFourRange);
		tvLowst = (TextView) candleView.findViewById(R.id.tvCandleLowRange);
		
		tvCNum = (TextView) candleView.findViewById(R.id.tvCandleNum);
		tvCTm = (TextView) candleView.findViewById(R.id.tvCandleTm);
		
		btnTen = (Button) candleView.findViewById(R.id.btnTenMinute);
		btnTen.setBackgroundColor(Util.getResourcesColor(R.color.red));
		btnTen.setTextColor(Util.getResourcesColor(R.color.white));
		btnTen.setOnClickListener(this);
		btnDay = (Button) candleView.findViewById(R.id.btnDay);
		btnDay.setOnClickListener(this);
		btnWeek = (Button) candleView.findViewById(R.id.btnWeek);
		btnWeek.setOnClickListener(this);
		btnMonth = (Button) candleView.findViewById(R.id.btnMonth);
		btnMonth.setOnClickListener(this);
		
		mCandle = (CandleView) candleView.findViewById(R.id.mCandle);
		mCandle.setContext(getActivity());
		
		vectorView = (VectorCandleView) candleView.findViewById(R.id.kVector);
		vectorView.setFocusable(false);
		
		mCandle.setVctView(vectorView);
	}
	/**
	 * 设定左边的峰值
	 */
	private void setPeakValue(List<K_CandleEntity> candles){

		if(candles.size()>0){
			double max = candles.get(0).getHigh();
			double min = candles.get(0).getLow();
			for(K_CandleEntity c:candles){
				if(max<c.getHigh()){
					max = c.getHigh();
				}
				if(min>c.getLow()){
					min = c.getLow();
				}
			}
			//找到了最高价和最低价
			if(max == min){
				//如果最高价等于最低价，那么就证明要重订最高和最低价啦
				max = max+(max*0.1f);
				min = min - (min*0.1f);
			}
			max = max+(max*0.05f);
			min = min - (min*0.05f);
			//定好最高和最低就要开始设置文本的值了
			tvHighst.setText(Util.dealStrs(max+"", 3));
			tvSecond.setText(Util.dealStrs(((max+min)/2+max)/2+"", 3));
			tvThird.setText(Util.dealStrs((max+min)/2+"", 3));
			tvFour.setText(Util.dealStrs(((max+min)/2+min)/2+"", 3));
			tvLowst.setText(Util.dealStrs(min+"", 3));
			Grid grid = new Grid();
			grid.setMaxValue(Util.dealStrs(max+"", 3));
			grid.setMingValue(Util.dealStrs(min+"", 3));
			mCandle.setGrid(grid);
		}else{
			tvHighst.setText("0.00");
			tvSecond.setText("0.00");
			tvThird.setText("0.00");
			tvFour.setText("0.00");
			tvLowst.setText("0.00");
			Grid grid = new Grid();
			grid.setMaxValue("0.00");
			grid.setMingValue("0.00");
			mCandle.setGrid(grid);
		}	
	}
	/**
	 * 设置最大量
	 * @param candles
	 */
	private void setMaxNum(List<K_CandleEntity> candles){
		double max = 10f;
		if(candles.size()>0){
			for(K_CandleEntity c:candles){
				if(max<c.getSum_num()){
					max = c.getSum_num();
				}
			}			
		}
		Grid grid = new Grid();
		grid.setMaxValue(Util.dealStrs(max+"", 3));
		vectorView.setGrid(grid);
		vectorView.setData(datas);
	}
	/**
	 * 设定顶部的数值、收盘价、涨跌幅、最高值、最低值、开盘价
	 */
	private void setValues(int index){

		if(datas.size()>0){
			if(index>=datas.size()){
				index = datas.size()-1;
			}
			tvClose.setText(datas.get(index).getClose()+"");
			tvRange.setText(datas.get(index).getUdRange());			
			tvHigh.setText("高:"+datas.get(index).getHigh());
			tvLow.setText("低:"+datas.get(index).getLow()+"");
			tvOpen.setText("开:"+datas.get(index).getOpen()+"");
			//设置成交量
			tvCNum.setText(datas.get(index).getSum_num()+"");
			tvCTm.setText(datas.get(index).getDate());
		}else{
			tvClose.setText("0.00");
			tvRange.setText("0.00");
			tvHigh.setText("高:0.00");
			tvLow.setText("低:0.00");
			tvOpen.setText("开:0.00");
			//设置成交量
			tvCNum.setText("0.00");
		}
		
	}

	/**
	 * 日K请求
	 */
	private void kDayRequest(){
		if(!Util.isNetworkConnected()){
			mHandler.sendEmptyMessage(0);
			return;
		}
		K_DaysRequest request =  new K_DaysRequest();
		C_Result result = request.dayRequest();	
		String str = (String) result.mData;
		JSONArray jMsg = null;
		int message = result.getmResult();
		if(str!=null){			
			try {
				jMsg	= new JSONArray(str);
				if(jMsg!=null){
					if(jMsg.length()>0){
						days.clear();
						for(int i=0; i<jMsg.length(); i++){
							JSONObject obj = jMsg.getJSONObject(i);
							if(obj!=null){
								K_CandleEntity candle = new K_CandleEntity();
								if(!obj.isNull("open_prc")){
									String o = obj.getString("open_prc");
									candle.setOpen(Double.parseDouble(Util.dealStrs(o, 3)));
								}
								if(!obj.isNull("close_prc")){		
									String c = obj.getString("close_prc");
									candle.setClose(Double.parseDouble(Util.dealStrs(c, 3)));
								}
								if(!obj.isNull("max_prc")){
									String m = obj.getString("max_prc");
									candle.setHigh(Double.parseDouble(Util.dealStrs(m, 3)));
								}
								if(!obj.isNull("min_prc")){
									String m = obj.getString("min_prc");
									candle.setLow(Double.parseDouble(Util.dealStrs(m, 3)));
								}
								if(!obj.isNull("sum_num")){
									String sn = obj.getString("sum_num");
									candle.setSum_num(Double.parseDouble(Util.dealStrs(sn, 3)));
								}
								if(!obj.isNull("sum_prc")){
									String sp = obj.getString("sum_prc");
									candle.setSum_prc(Double.parseDouble(Util.dealStrs(sp, 3)));
								}
								if(!obj.isNull("tm")){
									candle.setDate(Util.utc2Local(obj.getString("tm")));
								}	
								if(!obj.isNull("quote_change")){
									candle.setDate(obj.getString("quote_change"));
								}else{
									candle.setUdRange("0.00%");
								}									
								days.add(candle);
							}
						}
					}
					message = 1;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}		
		mHandler.sendEmptyMessage(message);
	}
	
	private class K_DayTask extends AsyncTask<Void, Void, Void>{

		protected Void doInBackground(Void... arg0) {
			kDayRequest();
			return null;
		}		
	}
	/**
	 * 周K请求
	 */
	private void kWeekRequest(){
		K_WeeksRequest request =  new K_WeeksRequest();
		C_Result result = request.weekRequest();	
		String str = (String) result.mData;
		JSONArray jMsg = null;
		int message = result.getmResult();
		if(str!=null){			
			try {
				jMsg	= new JSONArray(str);
				if(jMsg!=null){
					if(jMsg.length()>0){
						weeks.clear();
						for(int i=0; i<jMsg.length(); i++){
							JSONObject obj = jMsg.getJSONObject(i);
							if(obj!=null){
								K_CandleEntity candle = new K_CandleEntity();
								if(!obj.isNull("open_prc")){
									String o = obj.getString("open_prc");
									candle.setOpen(Double.parseDouble(Util.dealStrs(o, 3)));
								}
								if(!obj.isNull("close_prc")){		
									String c = obj.getString("close_prc");
									candle.setClose(Double.parseDouble(Util.dealStrs(c, 3)));
								}
								if(!obj.isNull("max_prc")){
									String m = obj.getString("max_prc");
									candle.setHigh(Double.parseDouble(Util.dealStrs(m, 3)));
								}
								if(!obj.isNull("min_prc")){
									String m = obj.getString("min_prc");
									candle.setLow(Double.parseDouble(Util.dealStrs(m, 3)));
								}
								if(!obj.isNull("sum_num")){
									String sn = obj.getString("sum_num");
									candle.setSum_num(Double.parseDouble(Util.dealStrs(sn, 3)));
								}
								if(!obj.isNull("sum_prc")){
									String sp = obj.getString("sum_prc");
									candle.setSum_prc(Double.parseDouble(Util.dealStrs(sp, 3)));
								}
								if(!obj.isNull("tm")){
									candle.setDate(Util.utc2Local(obj.getString("tm")));
								}	
								if(!obj.isNull("quote_change")){
									candle.setDate(obj.getString("quote_change"));
								}else{
									candle.setUdRange("0.00%");
								}		
								weeks.add(candle);
							}
						}
					}
					message = 2;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}		
		mHandler.sendEmptyMessage(message);
	}
	private class K_WeekTask extends AsyncTask<Void, Void, Void>{

		protected Void doInBackground(Void... arg0) {
			kWeekRequest();
			return null;
		}		
	}
	/**
	 * 月K请求
	 */
	private void kMonthRequest(){
		K_MonthRequest request =  new K_MonthRequest();
		C_Result result = request.monthRequest();	
		String str = (String) result.mData;
		JSONArray jMsg = null;
		int message = result.getmResult();
		if(str!=null){			
			try {
				jMsg	= new JSONArray(str);
				if(jMsg!=null){
					if(jMsg.length()>0){
						months.clear();
						for(int i=0; i<jMsg.length(); i++){
							JSONObject obj = jMsg.getJSONObject(i);
							if(obj!=null){
								K_CandleEntity candle = new K_CandleEntity();
								if(!obj.isNull("open_prc")){
									String o = obj.getString("open_prc");
									candle.setOpen(Double.parseDouble(Util.dealStrs(o, 3)));
								}
								if(!obj.isNull("close_prc")){		
									String c = obj.getString("close_prc");
									candle.setClose(Double.parseDouble(Util.dealStrs(c, 3)));
								}
								if(!obj.isNull("max_prc")){
									String m = obj.getString("max_prc");
									candle.setHigh(Double.parseDouble(Util.dealStrs(m, 3)));
								}
								if(!obj.isNull("min_prc")){
									String m = obj.getString("min_prc");
									candle.setLow(Double.parseDouble(Util.dealStrs(m, 3)));
								}
								if(!obj.isNull("sum_num")){
									String sn = obj.getString("sum_num");
									candle.setSum_num(Double.parseDouble(Util.dealStrs(sn, 3)));
								}
								if(!obj.isNull("sum_prc")){
									String sp = obj.getString("sum_prc");
									candle.setSum_prc(Double.parseDouble(Util.dealStrs(sp, 3)));
								}
								if(!obj.isNull("tm")){
									candle.setDate(Util.utc2Local(obj.getString("tm")));
								}	
								if(!obj.isNull("quote_change")){
									candle.setDate(obj.getString("quote_change"));
								}else{
									candle.setUdRange("0.00%");
								}		
								months.add(candle);
							}
						}
					}
					message = 3;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}		
		mHandler.sendEmptyMessage(message);
	}
	
	private class K_MonthTask extends AsyncTask<Void, Void, Void>{

		protected Void doInBackground(Void... arg0) {
			kMonthRequest();
			return null;
		}		
	}
	/**
	 * 请求10分钟K线图
	 */
	private void sendTenWebSocketMessage(){	
		tId = 7200;
		timer = new Timer();	
		timer.schedule(new TimerTask() {
			public void run() {	
				Logger.i("", "开启10分钟请求计时器！");
				if(tId>=9000){
					tId =7200;
				}
				JSONObject obj = new JSONObject();
				try {					
					obj.put("_id_", tId+"");
					obj.put("action", "Trends.getTenMinInfo");					
					obj.put("stm", "0");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
				GlobalApplication.client.getSokect().sendTextMessage(obj.toString());
				tId += 1;
			}
		}, 3, 30000);
	}
	
	/**
	 * @param num 追加数据量
	 * @param start 起始位置
	 */
	private void addData(int num, int start){
		//追加数据			
		for(int i=0; i<num; i++){
			K_CandleEntity candle = new K_CandleEntity();
			String date = tens.get(start+i).getDate();
			candle.setOpen(tens.get(start+i).getClose());
			candle.setClose(tens.get(start+i).getClose());
			candle.setHigh(tens.get(start+i).getClose());
			candle.setLow(tens.get(start+i).getClose());
			candle.setSum_num(0);			
			candle.setUdRange("0");
			date = postponeTenMinue(date);//推迟10分钟
			candle.setDate(date);
			tens.add(start+i+1, candle);
		}		
	}
	
	/**
	 * 有多条数据
	 */
	private void moreThanOneData(){
		int size = tens.size();
		int index = tens.size();	
		//先追加开盘时间到第一条成交价的时间的数据
		int num = 0;
		for(int i=1; i<size; i++){
			num = getDiffTenMinue(tens.get(i-1).getDate(), tens.get(i).getDate())-1;
			addData(num, i-1);
			if(index>0){
				//证明后面还有数据，需要继续追加，这时候追加的位置要发生改变了	
				i = tens.size()-index+1;
				size = tens.size();
				index--;
			}			
		}
		//数据加载完了还要计算当前时间和最后一条数据之间的数据,这个时候是往集合里面直接加入数据
		size = tens.size();
		num = getDiffTenMinue(tens.get(size-1).getDate(), svTime);
		addData(num, size-1);
	}
	
	/**
	 * 计算两个时间的十分钟差
	 */
	private int getDiffTenMinue(String start, String end){
		int num = 0;
		Date starts = DateUtil.parse(start);
		Date ends = DateUtil.parse(end);
		if(starts!=null&&ends!=null){
			long diff = (ends.getTime() - starts.getTime());
			num = (int) (diff/600000);
		}
		return num;
	}
	/**
	 * 往后推迟十分钟
	 */
	private String postponeTenMinue(String time){
		Date tm = DateUtil.parse(time);
		if(tm!=null){
			long t = tm.getTime()+600000;
			Date d = new Date(t);
			time = DateUtil.format(d);
		}
		return time;
	}
	/**
	 * 往后推迟12个小时
	 */
	private String postponeOpenTime(String time){
		Date tm = DateUtil.parse(time);
		if(tm!=null){
			long t = tm.getTime()+43200000;
			Date d = new Date(t);
			time = DateUtil.format(d);
		}
		return time;
	}
	
	/**
	 * 仅有一条数据
	 */
	private void onlyOneData(){
		int num = getDiffTenMinue(tens.get(0).getDate(), svTime);
		addData(num, 0);
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btnTenMinute:
				if(showChart==0){
					return;
				}
				showChart = 0;
				mHandler.sendEmptyMessage(10);
				btnDay.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnDay.setTextColor(Util.getResourcesColor(R.color.black));
				btnTen.setBackgroundColor(Util.getResourcesColor(R.color.red));
				btnTen.setTextColor(Util.getResourcesColor(R.color.white));
				btnWeek.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnWeek.setTextColor(Util.getResourcesColor(R.color.black));
				btnMonth.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnMonth.setTextColor(Util.getResourcesColor(R.color.black));
				break;
			case R.id.btnDay:
				if(showChart==1){
					return;
				}
				showChart = 1;
				btnDay.setBackgroundColor(Util.getResourcesColor(R.color.red));
				btnDay.setTextColor(Util.getResourcesColor(R.color.white));
				btnTen.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnTen.setTextColor(Util.getResourcesColor(R.color.black));
				btnWeek.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnWeek.setTextColor(Util.getResourcesColor(R.color.black));
				btnMonth.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnMonth.setTextColor(Util.getResourcesColor(R.color.black));
				new K_DayTask().execute();
				break;
			case R.id.btnWeek:
				if(showChart==2){
					return;
				}
				showChart = 2;
				btnDay.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnDay.setTextColor(Util.getResourcesColor(R.color.black));
				btnTen.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnTen.setTextColor(Util.getResourcesColor(R.color.black));
				btnWeek.setBackgroundColor(Util.getResourcesColor(R.color.red));
				btnWeek.setTextColor(Util.getResourcesColor(R.color.white));
				btnMonth.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnMonth.setTextColor(Util.getResourcesColor(R.color.black));
				new K_WeekTask().execute();
				break;
			case R.id.btnMonth:
				if(showChart==3){
					return;
				}
				showChart = 3;
				btnDay.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnDay.setTextColor(Util.getResourcesColor(R.color.black));
				btnMonth.setBackgroundColor(Util.getResourcesColor(R.color.red));
				btnMonth.setTextColor(Util.getResourcesColor(R.color.white));
				btnWeek.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnWeek.setTextColor(Util.getResourcesColor(R.color.black));
				btnTen.setBackground(getResources().getDrawable(R.drawable.btn_candle));
				btnTen.setTextColor(Util.getResourcesColor(R.color.black));
				new K_MonthTask().execute();
				break;
		}		
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if(timer!=null){
			timer.cancel();
			timer = null;
		}
		if(tenReceiver!=null){
			getActivity().unregisterReceiver(tenReceiver);
			tenReceiver = null;
		}
		if(moveReceiver!=null){
			getActivity().unregisterReceiver(moveReceiver);
			moveReceiver = null;
		}
		if(t_infoReceiver!=null){
			getActivity().unregisterReceiver(t_infoReceiver);
			t_infoReceiver = null;
		}
	}
}
